import 'dart:async';

import 'package:chathouse/pages/base/base.dart';
import 'package:chathouse/pages/expert/wallet_page.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/public/NetworkImageBrowser.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:chathouse/widgets/base_appbar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../chat/chat_page.dart';
import '../../model/expert_service_bean.dart';
import '../../model/masterplateModel.dart';
import '../../public/empty_widget.dart';
import '../../public/event_bus.dart';
import 'add_masterplate_page.dart';

class Masterplate extends StatefulWidget {
  const Masterplate({Key? key}) : super(key: key);

  @override
  _MasterplateState createState() => _MasterplateState();
}

class _MasterplateState extends State<Masterplate> {
  final RefreshController _refreshController =
      RefreshController(initialRefresh: true); // 刷新控件控制
  masterplateModel? model;
  StreamSubscription? refreshSubscription;

  @override
  void initState() {
    super.initState();
    load();
    refreshSubscription = eventBus.on<UpdateMemberInfo>().listen((event) {
      load();
    });
  }

  load() {
    Obj.showLoading();
    Apis.templateList({}).then((response) {
      Obj.dismissLoading();
      _refreshController.refreshCompleted();
      // masterplateModel();
      var data = response?.data;
      model = masterplateModel.fromJson(data);
      setState(() {});
    }).onError((error, stackTrace) {
      _refreshController.refreshCompleted();
      Obj.dismissLoading();
    });
  }

  Widget item(idx) {
    Rows item = model!.rows![idx];
    return InkWell(
      onTap: (){
        Get.to(AddMasterpl(item));
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w,vertical: 12.w),
        // constraints:  BoxConstraints(minHeight: 100.w), // 設置最小高度為 200
        height: 126.w,
        child: Row(
          children: [
            Expanded(
                child: Column(
              children: [
                Expanded(
                  child: Container(
                    width: double.infinity,
                    alignment: Alignment.centerLeft,
                    child: Text(
                      item.serviceName ?? "",
                      textAlign: TextAlign.left,
                      style: TextStyle(fontSize: 24.w),
                      maxLines: 1,
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    width: double.infinity,
                    alignment: Alignment.centerLeft,
                    child: Text(
                      item.templateComment ?? "",
                      textAlign: TextAlign.left,
                      style: TextStyle(fontSize: 20.w),
                      maxLines: 1,
                    ),
                  ),
                )
              ],
            )),
            SizedBox(
              width: 20.w,
            ),
            Icon(
              Icons.chevron_right,
              size: 20.w,
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar(title: "報價單模本"),
      body: model?.rows?.length == 0
          ? Align(
              child: Center(
                child: Container(
                    padding:
                        EdgeInsets.symmetric(horizontal: 32.w, vertical: 16.w),
                    // color: Styles.subMainColor,
                    decoration: BoxDecoration(
                        color: Styles.mainColor,
                        borderRadius: BorderRadius.circular(Styles.magin_16)),
                    child: Text(
                      "新增模本",
                      style: TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.w500,
                          fontSize: 20.w),
                    )),
              ).setOnClickListener(() {
                Get.to(AddMasterpl(null));
              }),
            )
          : SmartRefresher(
              enablePullDown: true,
              enablePullUp: false,
              controller: _refreshController,
              onRefresh: load,
              child: Column(
                children: [
                  Expanded(
                    child: ListView.separated(
                        shrinkWrap: true,
                        itemCount: model?.rows?.length ?? 0,
                        itemBuilder: (context, index) {
                          return item(index);
                        },
                        separatorBuilder: (context, index) {
                          return Container(
                            padding: EdgeInsets.symmetric(horizontal: 24.w),
                            height: 0.6,
                            color: Styles.c_ccc,
                          );
                        }),
                  ),
                  Container(
                      margin: EdgeInsets.only(bottom: 32.w),
                      padding: EdgeInsets.symmetric(
                          horizontal: 42.w, vertical: 12.w),
                      // color: Styles.subMainColor,
                      decoration: BoxDecoration(
                          color: Styles.mainColor,
                          borderRadius: BorderRadius.circular(Styles.magin_16)),
                      child: Text(
                        "新增模本",
                        style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.w500,
                            fontSize: 20.w),
                      )).setOnClickListener(() {
                    Get.to(AddMasterpl(null));
                  }),
                ],
              ),
            ),
    );
  }
}
